/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.autoescola.core.dao.impl;

import com.autoescola.core.dao.CrudDao;
import com.autoescola.core.dao.DaoImpl;
import com.autoescola.core.dao.DefaultDao;
import com.autoescola.core.dao.config.PersistenceContextConfig;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author leonardo luz fernandes
 * @version 0.1
 * @since 08/11/2010
 */

@DefaultDao
public abstract class CrudDaoImpl implements CrudDao<Object,Object>{

    @PersistenceContext(name=PersistenceContextConfig.PERSISTENCE_CONTEXT_DEFAULT_NAME)
    private EntityManager entityManager;

    @Override
    public Object load(Object key) throws Exception {
        return this.entityManager.find(key.getClass(), key);
    }//end method

    @Override
    public Object save(Object entity) throws Exception {
        this.entityManager.persist(entity);
        return entity;
    }//end method

    @Override
    public Object saveOrUpdate(Object entity) throws Exception {
        this.entityManager.persist(entity);
        return entity;
    }//end method

    @Override
    public Object update(Object entity) throws Exception {
        this.entityManager.persist(entity);
        return entity;
    }//end method

    @Override
    public boolean delete(Object key) throws Exception {
        this.entityManager.remove(key);
        return true;
    }//end method
    
    
    private boolean isNotNull( Object object , boolean on ) throws Exception {
        
        if( object == null && on == true ){
            throw new IllegalArgumentException("Parameter cannot be null.");
        }//end if
        
        return object != null ;
        
    }//end method
    
}//end klazz
